package org.wishwork.api.purchase.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.purchase.upload.request.Item;
import org.wishwork.api.purchase.upload.request.PurchaseUploadRequest;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 采购单上传
 * @author: WangYX
 * @create: 2022-04-08 15:05
 * @Version: 1.0.0
 **/
public class PurchaseUploadApi extends Api<PurchaseUploadResponse, PurchaseUploadRequest> {

    private static final Logger log = LoggerFactory.getLogger(PurchaseUploadApi.class);

    private Context context;

    public PurchaseUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(PurchaseUploadRequest purchaseUploadRequest) throws Exception {
        String url = "/open/jushuitan/purchase/upload";

        Map<String, Object> params = new HashMap<>();
        if (ObjectUtil.isNotEmpty(purchaseUploadRequest.getIsConfirm())) {
            params.put("is_confirm", purchaseUploadRequest.getIsConfirm());
        }
        if (purchaseUploadRequest.getWmsCoId() != null) {
            params.put("wms_co_id", purchaseUploadRequest.getWmsCoId());
        }
        if (purchaseUploadRequest.getSupplierId() != null) {
            params.put("supplier_id", purchaseUploadRequest.getSupplierId());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getItemType())) {
            params.put("item_type", purchaseUploadRequest.getItemType());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getExternalId())) {
            params.put("external_id", purchaseUploadRequest.getExternalId());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getRemark())) {
            params.put("remark", purchaseUploadRequest.getRemark());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getTerm())) {
            params.put("term", purchaseUploadRequest.getTerm());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getSendAddress())) {
            params.put("send_address", purchaseUploadRequest.getSendAddress());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getPurchaserName())) {
            params.put("purchaser_name", purchaseUploadRequest.getPurchaserName());
        }
        if (purchaseUploadRequest.getTaxRate() != null) {
            params.put("tax_rate", purchaseUploadRequest.getTaxRate());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getPoDate())) {
            params.put("po_date", purchaseUploadRequest.getPoDate());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getLId())) {
            params.put("l_id", purchaseUploadRequest.getLId());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getLogisticsCompany())) {
            params.put("logistics_company", purchaseUploadRequest.getLogisticsCompany());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getReceiverState())) {
            params.put("receiver_state", purchaseUploadRequest.getReceiverState());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getReceiverCity())) {
            params.put("receiver_city", purchaseUploadRequest.getReceiverCity());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getReceiverDistrict())) {
            params.put("receiver_district", purchaseUploadRequest.getReceiverDistrict());
        }
        if (StrUtil.isNotEmpty(purchaseUploadRequest.getMergePoId())) {
            params.put("merge_po_id", purchaseUploadRequest.getMergePoId());
        }
        if (purchaseUploadRequest.getItems() != null && purchaseUploadRequest.getItems().size() > 0) {
            List<Map<String, Object>> list = new ArrayList<>();
            List<Item> items = purchaseUploadRequest.getItems();
            for (Item item : items) {
                Map<String, Object> map = new HashMap<>();
                if (StrUtil.isNotEmpty(item.getSkuId())) {
                    map.put("sku_id", item.getSkuId());
                }
                if (item.getQty() == null) {
                    map.put("qty", item.getQty());
                }
                if (ObjectUtil.isNotEmpty(item.getPrice())) {
                    map.put("price", item.getPrice());
                }
                if (StrUtil.isNotEmpty(item.getPlanArriveDate())) {
                    map.put("plan_arrive_date", item.getPlanArriveDate());
                }
                if (StrUtil.isNotEmpty(item.getRemark())) {
                    map.put("remark", item.getRemark());
                }
                list.add(map);
            }
            params.put("items", list);
        }
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected PurchaseUploadResponse process(JSONObject data, PurchaseUploadRequest purchaseUploadRequest) throws Exception {
        PurchaseUploadResponse purchaseUploadResponse = data.toJavaObject(PurchaseUploadResponse.class);
        return purchaseUploadResponse;
    }
}
